/*
 * Copyright (C) 2007 BitLab.com.ua
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ua.com.bitlab.rsprocessor;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;

/**
 * @author Yaroslav Yermak
 * @version 1.0, Dec 6, 2007
 */
class ReflectionHelper {
// -------------------------- STATIC METHODS --------------------------

    static boolean isPrimaryKeySet(Object currentObject) throws SQLException {
        Field[] fields = currentObject.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (!field.isAnnotationPresent(PrimaryKey.class)) {
                continue;
            }
            try {
                Method getter = currentObject.getClass().getDeclaredMethod("get" + upFirstLetter(field.getName()));
                return null != getter.invoke(currentObject);
            } catch (NoSuchMethodException e) {
                throw new SQLException("Field annotated as PrimaryKey has no getter", e);
            } catch (InvocationTargetException e) {
                throw new SQLException("Faild to invoke getter on PrimaryKey", e);
            } catch (IllegalAccessException e) {
                throw new SQLException("Getter of PrimaryKey has IllegalAccess", e);
            }
        }
        throw new SQLException("Class [" + currentObject.getClass() + "] has no PrimaryKey annotation");
    }


    static void setPrimaryKey(Object currentObject, Object primaryKey) throws SQLException {
        Field[] fields = currentObject.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (!field.isAnnotationPresent(PrimaryKey.class)) {
                continue;
            }
            try {
                Method[] methods = currentObject.getClass().getDeclaredMethods();
                for (Method method : methods) {
                    if (method.getName().equals("set" + upFirstLetter(field.getName()))) {
                        method.invoke(currentObject, primaryKey);
                        return;
                    }
                }
            } catch (InvocationTargetException e) {
                throw new SQLException("Faild to invoke setter on PrimaryKey", e);
            } catch (IllegalAccessException e) {
                throw new SQLException("Setter of PrimaryKey has IllegalAccess", e);
            }
        }
        throw new SQLException("Class [" + currentObject.getClass() + "] has no PrimaryKey annotation");
    }

    private static String upFirstLetter(String name) {
        return Character.toUpperCase(name.charAt(0)) + name.substring(1);
    }

}
